<?php 
namespace app\admin\model;
use think\Model;
use app\admin\model\Pops;
use app\admin\model\Clouds;
use app\admin\model\Areas;
class Networks extends Model{
	public $surname = null;
	const NETWORK_TYPE_2 = 2; //数据中心
	const NETWORK_TYPE_3 = 3; //公有云
	/*本端接入类型*/
	public function getTypeList($index=null){
		$typeList = array(2=>lang('Data Center'),3=>lang('Public Cloud'));
		if($index === null){
			return $typeList;
		}
		return isset($typeList[$index]) ? $typeList[$index] : '';
	}
	
	/*获取pop点*/
	public function getPOPS(){
		$areas = new Areas;
		return $areas->where(array('status'=>1))->order('sort asc')->field('id,name')->select();
	}
	public function getTypelabelAttr($value){
		return $this->getTypeList($value);
	}
	/*费用*/
	public function getTotalAttr($value,$data){

		return $value > 0 ? $value = $data['currency']." ". _discount($value) : '--';//折扣
	}
	public function getStatuslabelAttr($value=null){
		$statusLabel = array(1=>lang('Application'),2=>lang('In Use'),3=>lang('Configuration'),4=>lang('Apply Return'),8=>lang('Adjustment'),10=>lang('Closeing'),12=>lang('Closed'),14=>lang('Close Back'),15=>lang('OutTime'),16=>lang('Finishs'));
		return isset($statusLabel[$value]) ? $statusLabel[$value] : $statusLabel;
	}
	public function getSnAttr($value){
		return empty($value) ? '' : $value;
	}
	/*到期日期*/
	public function getEnddateAttr($value){
		return empty($value) ? '--' : date('Y-m-d',$value);
	}
	/*本端接入地区*/
	public function getLocatlabeladdressAttr($value,$data){
		if($value == 3){
			$address = Areas::getAreaLabel(array(intval($data['country_id']),intval($data['city_id'])));
		}else{
			$address = Areas::getItemAddressLabel($data['pop_id']);
		}
		return $address;
	}
	/*对端接入地区*/
	public function getEndlabeladdressAttr($value,$data){
		if($value == 3){
			$address = Areas::getAreaLabel(array(intval($data['ecountry_id']),intval($data['ecity_id'])));
		}else{
			$address = Areas::getItemAddressLabel($data['epop_id']);
		}
		return $address;
	}
	/*本端接入*/
	public function getLocallabelAttr($value,$data){
		$typeLable = $this->getTypeList($data['type']);
		if($data['type'] == 3){
			$name = Clouds::getNameLabel($value);
		}else{
			$name = Pops::getNameLabel($data['pop_id']);
		}
		return "【".$typeLable."】".$name;
	}
	/*对端接入*/
	public function getEndclouAttr($value,$data){
		$typeLable = $this->getTypeList($data['endtype']);
		if($data['endtype'] == 3){
			$name = Clouds::getNameLabel($value);
		}else{
			$name = Pops::getNameLabel($data['epop_id']);
		}
		return "【".$typeLable."】".$name;
	}
	/*购买时长*/
	public function dayLength($index=null){
		$dayLength = array(1=>lang('1 months'),3=>lang('3 months'),6=>lang('Half a year'),12=>lang('1 years'));
		if($index!=null){
			return isset($dayLength[$index]) ? $dayLength[$index] : '';
		}
		return $dayLength;
	}
	public function getApprotimeAttr($value){
		return empty($value) ? '--' : dateFormat($value);
	}
	/*获取旧数据*/
	public function getPreData($id){
		$data = db('network_logs')->where(array('network_id'=>$id,'status'=>8))->order('id desc')->value('old_data');
		return empty($data) ? null : unserialize($data);
	}
	public function setDataLabel($data){
		if($data['type'] == 3){
			$data['typelabel'] = \app\admin\model\Clouds::getName($data['local_cloud_id']);
		}else{
			$data['typelabel'] = $this->getTypeList($data['type']);
		}
		if($data['endtype'] == 3){
			$data['endlabel'] = \app\admin\model\Clouds::getName($data['endcloudid']);
		}else{
			$data['endlabel'] = $this->getTypeList($data['endtype']);
		}
		$data['total'] = _discount($data['total']);
		//$data['endarea'] = \app\admin\model\Clouds::getName($data['endcloudaddrid']);
		$data['daLength'] = $this->dayLength($data['daylength']);
		return $data;
	}

	public static function getFields($is_com=false){
		$fiels= array(
			'id'=>'ID',
			'sn' => lang('Customer No.'),
			'name'=>lang('Line Name'),
			'username' => lang('Username'),
			'mobile' => lang('Mobile'),
			'company' => lang('Company Name'),
			'locallabel' => lang('Local Access'),
			'local_network' => lang('End Network'),
			'endclou' => lang('Peer Access'),
			'end_network' => lang('End-to-end Network'),
			'daylength' => lang('Purchase Time'),
			'broadband' => lang(' Bandwidth'),
			'status' => lang('Status'),
			'enddate' => lang('Due Date'),

			
		);
		if(!$is_com){
			$users = array(
				'email' => lang('Customer Mailbox'),
				'tel' => lang('Customer Phone'),
				'company_address' => lang('Company Address'),
				'position' => lang('Customer Position'),
				'username' => lang('Username'),
			);
			$fiels= array_merge($fiels,$users);
		}
		return $fiels;
	}
	/*判断是否分配工程师*/
	public static function getEngStatus($network_id,$state=0){
		$status = 0;
		switch ($state) {
			case 10:
				$status = 10;
				break;
			case 8:
				$status = 8;
				break;
			case 1:
				$status = 1;
				break;
		}
		$map['network_id'] = $network_id;
		$map['is_update'] = 0;
		$status = db('network_enginee')->where($map)->where('state',$status)->order('id desc')->value('status');
		//file_put_contents('1111.log', db('network_enginee')->getLastSql());
		return (int)$status;
	}
	/*判断是否分配工程师*/
	public static function checkEngHasPsiee($network_id,$user_id){
		$map['network_id'] = $network_id;
		$map['enginee_id'] = $user_id;
		$eng = db('network_enginee')->where($map)->order('id desc')->find();
		return  $eng['status'] == 1 ? $eng['id'] : false;
	}
	/*判断是否分配工程师*/
	public static function lastEngId($network_id){
		return db('network_enginee')->where('network_id',$network_id)->order('id desc')->value('enginee_id');
	}
	/*获取所有的工程师回馈*/
	public static function backeens($network_id){
		return db('network_enginee')->alias('n')
				->join('users u','u.admin_id = n.enginee_id')
				->field('n.*,u.username')
				->where('n.network_id',$network_id)->order('n.create_time desc')->select();
	}
	/*设置过期的线路*/
	public function setOutTimeNetwork(){
		$mat['enddate'] = array('lt',strtotime(date('Y-m-d',time())));
		$mat['status'] = array('in',[1,2,8,4,10]);
		db('networks')->where('enddate>0')->where($mat)->update(['status'=>15]);
	}
}
?>